#include "common_api.h"
#include "pmc.h"
#include "time_mgr.h"

unsigned int crc_table[256];  
    
/* 
**initialize the cre32 table 
*/  
void init_crc_table(void)  
{  
    unsigned int c;  
    unsigned int i, j;  
      
    for (i = 0; i < 256; i++) {  
        c = (unsigned int)i;  
        for (j = 0; j < 8; j++) {  
            if (c & 1)  
                c = 0xedb88320L ^ (c >> 1);  
            else  
                c = c >> 1;  
        }  
        crc_table[i] = c;  
    }  
}  
  
/*compute the crc32 code for buffer*/  
unsigned int crc32(unsigned int crc,unsigned char *buffer, unsigned int size)  
{  
    unsigned int i;  
    for (i = 0; i < size; i++) {  
        crc = crc_table[(crc ^ buffer[i]) & 0xff] ^ (crc >> 8);  
    }  
    return crc ;  
}
/*
uint32_t check_sum(uint8_t* buff, uint16_t len)
{
    uint16_t i;
    uint32_t r_sum = 0;
    for(i = 0; i < len; i++)
    {
        r_sum += buff[i];
    }
    
    return r_sum;
}*/

/*****************************************************************************
** Function name:
**      sys_tick_delay_ms

** Descriptions:
**      using the systick to do delay some ms
**
** parameters:
**      dly_ms------the millisecond to delay
**
** Returned value:
**      none
** note:
**      MAX delay time is 1597ms
**
*****************************************************************************/
void sys_tick_delay_ms(uint32_t dly_ms)
{
      SysTick_CTRL &= ~((1U<<16)|0x00000007);
      SysTick_LOAD  = (dly_ms*SYS_TICK_1ms)&0x00ffffff;

      SysTick_CTRL  |=  1U<<0;
      while((SysTick_CTRL&(1U<<16)) == 0); 
}

/*****************************************************************************
** Function name:
**      sys_tick_delay_us

** Descriptions:
**      using the systick to do delay some us
**
** parameters:
**      dly_us------the microsecond to delay
**
** Returned value:
**      none
** note:
**      MAX delay time is 1597830us
**
*****************************************************************************/
void sys_tick_delay_us(uint32_t dly_us)
{
      SysTick_CTRL &= ~((1U<<16)|0x00000007);

      SysTick_LOAD  = (dly_us*SYS_TICK_1us)&0x00ffffff;

      SysTick_CTRL  |=  1U<<0;
      while((SysTick_CTRL&(1U<<16)) == 0); 
}
/*****************************************************************************/

void wait_ms(uint32_t delay)
{
    //uint32_t start_time, stop_time;
    //start_time = get_system_time();  
    //stop_time = get_system_time();
  
    start_temp_timer();
    
    while((Is_temp_time_over(delay) == false));
    //{
        //stop_time = get_system_time();
    //}
    stop_temp_timer();
}

uint16_t common_mem_cpy(uint8_t* src, uint8_t* dst, uint16_t len)
{
    uint16_t i;
    
    for(i = 0; i < len; i++)
    {
        dst[i] = src[i];
    }
    
    return len;
}

bool common_mem_cmp(uint8_t* a, uint8_t* b, uint16_t len)
{
    uint16_t i;
    
    for(i = 0; i < len; i++)
    {
        if(a[i] != b[i])
        {
            return false;
        }
    }
    
    return true;
}

void common_mem_xor(uint8_t* dst, uint8_t* msk, uint16_t len)
{
    uint16_t i;
    
    for(i = 0; i < len; i++)
    {
        dst[i] ^= msk[i];
    }
}

